package com.yuantu.judiciary.serviceimpl;

import com.yuantu.judiciary.dao.IAccountDao;
import com.yuantu.judiciary.dao.IOrganizationSourceDao;
import com.yuantu.judiciary.dao.ISysPlatformOperateLogDao;
import com.yuantu.judiciary.model.domain.OrganizationSourceDO;
import com.yuantu.judiciary.model.domain.SysPlatformOperateLogDO;
import com.yuantu.judiciary.model.vo.DiscountCodeInfoVO;
import com.yuantu.judiciary.model.vo.OrgSourceVO;
import com.yuantu.judiciary.model.vo.PageVO;
import com.yuantu.judiciary.model.vo.SysPlatformOperateLogVO;
import com.yuantu.judiciary.service.IOrganizationSourceService;
import com.yuantu.judiciary.service.ISysPlatformOperateLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 组织管理模块
 *
 * @author syw
 * @since 2023-08-12
 **/
@Service
@RequiredArgsConstructor
public class SysPlatformOperateLogServiceImpl implements ISysPlatformOperateLogService {


    private final ISysPlatformOperateLogDao sysPlatformOperateLogDao;

    private final IAccountDao accountDao;


    @Override
    public void addSysPlatformOperateLog(SysPlatformOperateLogDO sysPlatformOperateLog) {
        sysPlatformOperateLogDao.save(sysPlatformOperateLog);
    }

    @Override
    public PageVO<SysPlatformOperateLogVO> sysPlatformLogList(String param, Pageable pageable) {
        Page<SysPlatformOperateLogDO> operateLogPage = sysPlatformOperateLogDao.findInfosByParam(param, pageable);
        List<SysPlatformOperateLogDO> content = operateLogPage.getContent();
        if (CollectionUtils.isEmpty(content)) {
            return new PageVO<>(operateLogPage.getTotalElements(), Collections.emptyList());
        }
        List<Long> operatorIds = content.stream().map(SysPlatformOperateLogDO::getOperatorId).collect(Collectors.toList());
        Map<Long, IAccountDao.AccountUserInfo> userInfoMap = accountDao.getAccountInfo(operatorIds).stream().
                collect(Collectors.toMap(IAccountDao.AccountUserInfo::getAccountId, Function.identity(), (k, v) -> v));
        List<SysPlatformOperateLogVO> result = new ArrayList<>();
        for (SysPlatformOperateLogDO sysPlatformOperateLogDO : content) {
            SysPlatformOperateLogVO sysPlatformOperateLogVO = new SysPlatformOperateLogVO();
            sysPlatformOperateLogVO.setCreateTime(sysPlatformOperateLogDO.getCreateTime());
            sysPlatformOperateLogVO.setDescription(sysPlatformOperateLogDO.getDescription());
            sysPlatformOperateLogVO.setId(sysPlatformOperateLogDO.getId());
            sysPlatformOperateLogVO.setOperatorId(sysPlatformOperateLogDO.getOperatorId());
            Long operatorId = sysPlatformOperateLogDO.getOperatorId();
            IAccountDao.AccountUserInfo accountUserInfo = userInfoMap.get(operatorId);
            if(accountUserInfo != null){
                sysPlatformOperateLogVO.setOperatorName(accountUserInfo.getUsername());
            }
            sysPlatformOperateLogVO.setOperatorIp(sysPlatformOperateLogDO.getOperatorIp());
            result.add(sysPlatformOperateLogVO);
        }
        return new PageVO<>(operateLogPage.getTotalElements(), result);
    }
}
